package com.example.dataresult;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText etAccount;
    private EditText etPassword;
    private Button btnLogin;
    private TextView tvShowResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        tvShowResult = findViewById(R.id.tv_show_result);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, VerifyActivity.class);

                //存入数据
                Log.i("msg", "onClick: " + etAccount.getText().getClass());
                intent.putExtra("account", etAccount.getText().toString());
                intent.putExtra("password", etPassword.getText().toString());
                startActivityForResult(intent,1);  //requestCode：请求码
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null){
            if (resultCode == 0){
                //如果返回码为1，说明验证码输入成功
                if (requestCode == 1){
                    String returnData = data.getStringExtra("result");
                    Toast.makeText(this, returnData, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
        Toast.makeText(this, "登录失败！", Toast.LENGTH_SHORT).show();
    }
}
